package cn.gov.ssxmzj.service;

import cn.gov.ssxmzj.entry.BeneficiaryInfo;
import cn.gov.ssxmzj.entry.PageNews;
import cn.gov.ssxmzj.entry.Result;
import cn.gov.ssxmzj.mapper.BeneficiaryMapper;
import cn.gov.ssxmzj.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BeneficiaryService {

    @Autowired
    private BeneficiaryMapper mapper;

    /**
     * 获取分页获取受惠人信息
     * @param page
     * @param limit
     * @param beneficiaryType
     * @param villageId
     * @return
     */
    public PageNews getBeneficiaryInfo(int page,int limit,String beneficiaryType,String villageId,String searchName){
        page = 0 == page ? 0 : --page * limit;
        beneficiaryType = Utils.isEmpty(beneficiaryType)?null:beneficiaryType;
        villageId = Utils.isEmpty(villageId)?null:villageId;
        Map sqlMap = Utils.asMap("beneficiaryType",beneficiaryType,"villageId",villageId,"validTag","1");
        //如果是搜索,则放入搜索名字
        if(!Utils.isEmpty(searchName)){
            sqlMap.put("searchName","%"+searchName+"%");
        }
        int count = mapper.getCount(sqlMap);
        if (count == 0) {
            return new PageNews(Result.SERVICE_ERROR,count,"[]","未查询到数据");
        }
        sqlMap.putAll(Utils.asMap("page",page,"limit",limit));
        List<BeneficiaryInfo> beneficiaryInfoList = mapper.getBeneficiaryInfo(sqlMap);
        return new PageNews("0",count,beneficiaryInfoList,"查询成功");
    }
}
